# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request
from scrapy.loader import ItemLoader
from ..items import ImoocFreeCourseListItem
from datetime import datetime
from urllib import parse


class ImoocFreeSpider(scrapy.Spider):
    name = 'imooc-free'
    allowed_domains = ['imooc.com']
    start_urls = ['https://www.imooc.com/course/list/']

    def parse(self, response):
        for course_card in response.css('.course-card-container'):
            imooc_free_item = ItemLoader(item=ImoocFreeCourseListItem(), selector=course_card)
            imooc_free_item.add_css('course_url', '.course-card::attr(href)')
            imooc_free_item.add_css('course_num', '.course-card::attr(href)')
            imooc_free_item.add_css('course_img_url', '.course-banner::attr(data-original)')
            imooc_free_item.add_css('course_name', '.course-card-name::text')
            imooc_free_item.add_css('course_level', '.course-card-info span:nth-child(1)::text')
            imooc_free_item.add_css('course_student_num', '.course-card-info span:nth-child(2)::text')
            imooc_free_item.add_css('course_short_desc', '.course-card-desc::text')
            imooc_free_item.add_css('course_price', '.price::text')
            imooc_free_item.add_css('course_cost_price', '.cost-price::text')
            imooc_free_item.add_css('course_label', '.course-label label::text')
            imooc_free_item.add_value('created_at', datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            yield imooc_free_item.load_item()
        # 下一页
        # next_page_url = ''
        # for page_a in response.css('.page a'):
        #     if page_a.css('::text').get() == '下一页':
        #         next_page_url = parse.urljoin(response.url, page_a.css('::attr(href)').get())
        #
        # if next_page_url != '':
        #     yield Request(url=next_page_url, callback=self.parse)
